﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            string nodeId = Request["nodeId"];
            if(nodeId != null && nodeId != "")
            {
                lblContent.Text = "You have navigated to a page that shows node " + nodeId;

                // in order to expand the tree automagically, we need to specify the nodes to open
                List<int> parentIds = new List<int>();

                parentIds = getParentIds(int.Parse(nodeId));

                // now with the data at hand, we need run some scripts to tell the tree to start expanding
                StringBuilder stb = new StringBuilder();
                stb.Append("<script>\n");
                stb.Append("var ids = new Array();\n");
                foreach (int id in parentIds)
                {
                    stb.Append("ids[ids.length] = " + id + "\n");
                }
                stb.Append("tree_setSelectedNode(" + nodeId + ");\n");
                stb.Append("tree_setNodeExpansionQueue(ids);\n");
                stb.Append("</script>");

                ClientScript.RegisterStartupScript(this.GetType(), "treeInitializers", stb.ToString());
            }
        }
    }

    /// <summary>
    /// Sample function which emulates a call to your database to retrieve a list of parents 
    /// starting from <paramref name="nodeId"/> and ending with the root (id 0).
    /// </summary>
    /// <param name="nodeId"></param>
    /// <returns></returns>
    private List<int> getParentIds(int nodeId)
    {
        List<int> result = new List<int>();


        switch (nodeId)
        {
            case 4: // Orange
                result.Add(1);
                break;
            case 5: // Apple
                result.Add(1);
                break;
            case 6: // Monkey
                result.Add(3);
                break;
            case 7: // Fish
                result.Add(3);
                break;
            case 8: // Bird
                result.Add(3);
                break;
            case 9: // Mandrill
                result.Add(3);
                result.Add(6);
                break;
            case 10: // Baboon
                result.Add(3);
                result.Add(6);
                break;
            case 11: // Gorilla
                result.Add(3);
                result.Add(6);
                break;
            case 12: // Chimpanzee
                result.Add(3);
                result.Add(6);
                break;
        }
        result.Add(0);

        return result;
    }
}
